home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / mtools.lha / mtools-2.0.7 / buf_read.c next >
C/C++ Source or Header  |  1992-09-10  |  2KB  |  75 lines

  1. /*
  2.  * Do full cylinder buffered reads from slow devices.  Uses a simple
  3.  * buffered read/delayed write algorithm.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "msdos.h"
  8.  
  9. unsigned char *disk_buf;        /* disk read/write buffer */
  10. int disk_size;                /* size of read/write buffer */
  11. long disk_current;            /* first sector in buffer */
  12. int disk_dirty;                /* is the buffer dirty? */
  13.  
  14. extern int fd;
  15. extern long disk_offset;
  16.  
  17. void
  18. disk_read(start, buf, len)
  19. long start;
  20. unsigned char *buf;
  21. int len;
  22. {
  23.     register long i;
  24.     int length;
  25.     unsigned char *buf_ptr, *disk_ptr;
  26.     char *memcpy();
  27.     long where, tail, lseek();
  28.     void perror(), exit(), disk_flush();
  29.  
  30.                     /* don't use cache? */
  31.     if (disk_size == 1) {
  32.         where = (start * MSECTOR_SIZE) + disk_offset;
  33.         if (lseek(fd, where, 0) < 0) {
  34.             perror("disk_read: lseek");
  35.             exit(1);
  36.         }
  37.                     /* read it! */
  38.         if (read(fd, (char *) buf, (unsigned int) len) != len) {
  39.             perror("disk_read: read");
  40.             exit(1);
  41.         }
  42.         return;
  43.     }
  44.  
  45.     tail = start + (len / MSECTOR_SIZE) - 1;
  46.     for (i = start; i <= tail; i++) {
  47.                     /* a "cache" miss */
  48.         if (i < disk_current || i >= disk_current + disk_size) {
  49.  
  50.             if (disk_dirty)
  51.                 disk_flush();
  52.  
  53.             disk_current = (i / disk_size) * disk_size;
  54.             where = (disk_current * MSECTOR_SIZE) + disk_offset;
  55.             length = disk_size * MSECTOR_SIZE;
  56.  
  57.                     /* move to next location */
  58.             if (lseek(fd, where, 0) < 0) {
  59.                 perror("disk_read: lseek");
  60.                 exit(1);
  61.             }
  62.                     /* read it! */
  63.             if (read(fd, (char *) disk_buf, (unsigned int) length) != length) {
  64.                 perror("disk_read: read");
  65.                 exit(1);
  66.             }
  67.         }
  68.                     /* a cache hit... */
  69.         buf_ptr = buf + ((i - start) * MSECTOR_SIZE);
  70.         disk_ptr = disk_buf + ((i - disk_current) * MSECTOR_SIZE);
  71.         memcpy((char *) buf_ptr, (char *) disk_ptr, MSECTOR_SIZE);
  72.     }
  73.     return;
  74. }
  75.